package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.AbilitaManagerRemote;
import sessions.UserManagerRemote;
import staticHtml.PaginaStatica;
import entities.Utente;

public class PaginaPersonaleServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		mostraPagina(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		mostraPagina(request, response);
	}

	private void mostraPagina(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		try{
			Hashtable<String,String> env = new Hashtable<String,String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);

			Object ref = jndiContext.lookup("UserManager/remote");
			UserManagerRemote u = (UserManagerRemote) ref;

			Object ref2 = jndiContext.lookup("AbilitaManager/remote");
			AbilitaManagerRemote a = (AbilitaManagerRemote) ref2;


			response.setContentType("text/html");

			PrintWriter out = response.getWriter();

			Utente user;

			Utente utente=(Utente) request.getSession().getAttribute("utente");
			String targetUsername=request.getParameter("name");
			String external = request.getParameter("external");
			
			String isModificaDati;
			String isHidden="";
			// controllo per la visualizzazione corretta della pagina
			if(utente==null)
			{
				
				out.println(PaginaStatica.top());
				if(external!=null){
					user= u.getUserWithUsername(targetUsername);
					isHidden = "class='hidden'";
					isModificaDati="";
				}else
				{

					out.println("Sessione scaduta<br><a href='index.html'> Torna alla home </a>");
					out.println(PaginaStatica.bottom());
					return;
				}

			}
			else
			{

				out.println(PaginaStatica.topUser());
				if(targetUsername==null)
				{	
					user=(Utente) request.getSession().getAttribute("utente");
					isHidden = "class='hidden'";
					isModificaDati="<form action='./modificaDati.html' class='modifica'>"+
							"<input type='submit' value='Modifica dati'>"+
							"</form>";

				}
				else
				{
					user= u.getUserWithUsername(targetUsername);
					
					isModificaDati="";

				}

			}

			out.println("<div id='titolo'>"+
					"<label class='titolo'>"+user.getUserName()+"</label> <label class='feedback'>"+user.getFeedback());
			out.println("</label>"+
					"</div>"+
					"<div id='info'>"+
					"<div class='section'>"+
					"<label>Dati Personali </label>	</div>	<div>"+
					"<label class='desc'>Nome : </label> <label class='value'>"+user.getNome());
			out.println("</label></div>	<div>"+
					"<label class='desc'>Cognome : </label> <label class='value'>"+user.getCognome());
			out.println("</label></div>	<div>"+
					"<label class='desc'>Data di Nascita : </label> <label class='value'>"+user.getDataDiNascita());
			out.println("</label></div>	<div>"+
					"<label class='desc'>email : </label> <label class='value'>"+ user.getEmail());
			out.println("</label></div>	</div><div id='abilita'>"+
					"<div class='section'>"+
					"<label>Abilita' </label></div>");

			List<String> abilita = a.getAbilita(user);

			for(String nomeAbilita:abilita)
			{

				out.println("<form method='post' action='./richiestaAiuto.html'>"+
						"<label class='skill'>"+nomeAbilita+"</label>");
				
					
					out.println("<input type='submit' "+isHidden+" value='contatta'>");
				
				out.println("<input type=hidden name='destinatario' value="+targetUsername+">"+
						"<input type=hidden name='abilita' value="+nomeAbilita+">"+
						"</form>");							
			}

			out.println("</div>"+isModificaDati);
			out.println(PaginaStatica.bottom());


		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
}
